package cn.ml.chuji.oop;

import cn.ml.entity.Student;

import java.util.List;
import java.util.Random;

public class ExceptionTest {

    public static void main(String[] args) {
        List<Student> studentList = Student.getStudentList();

        try {
            //test(studentList);
            test2(studentList);
        } catch (Exception e){
            System.out.println("main---异常");
            throw new RuntimeException("第一层异常" + "main异常");
        }

        System.out.println("ok已雇佣.......");
    }

    public static void test(List<Student> studentList){

        String gg = null;
        Random random = new Random();
        while (true) {
            if (studentList.size() == 0){
                System.out.println("无可用的人");
                throw new RuntimeException("无可用的人222");
            }
            int index = random.nextInt(studentList.size());
            Student student = studentList.get(index);
            studentList.remove(index);
            try {
                gg(student);
                System.out.println("此人可以用");
                break;
            }catch (Exception e) {
                System.out.println("此人不能用，重新");
                throw new RuntimeException("第222层异常---");
            }
        }
        System.out.println("11111111111");
    }

    public static void test2(List<Student> studentList){
        String gg = null;
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.size() == 0){
                System.out.println("无可用的人");
                throw new RuntimeException("无可用的人222");
            }
            Student student = studentList.get(i);
            try {
                gg(student);
                System.out.println("此人可以用");
                //break;
            }catch (Exception e) {
                System.out.println("此人不能用，重新");
                throw new RuntimeException("第222层异常---");
                //continue;
            }
        }

        System.out.println("11111111111");
    }

    public static void gg(Student student) {
        if ("张三1".equals(student.getName())) {
            System.out.println("张三1");
            throw new RuntimeException("第333层异常" + "此人不合适");
        }else if ("张三2".equals(student.getName())) {
            System.out.println("张三2");
            throw new RuntimeException("第333层异常" + "此人不合适");
        }else {
            System.out.println(student.getName());
            throw new RuntimeException("第333层异常" + "此人不合适");
        }
    }
}
